package org.hansk.wg.core.util
{
	import avmplus.getQualifiedClassName;
	
	import org.hansk.wg.core.IBase;
	import org.hansk.wg.core.data.LinkedStack;
	
	import flash.utils.Dictionary;
	
	import flashx.textLayout.elements.LinkState;

	/**
	 * @file CompFactory.as
	 * @explain 组件工厂
	 * @copyright it.hansk.org
	 * @author <a href="mailto:hans#hansk.org">Hans</a>
	 * @date 2013-8-14
	 */
	public class CompFactory implements IFactory
	{
		private var workshops:Dictionary;
		
		public function CompFactory()
		{
			workshops = new Dictionary();
		}
		/**
		 * 创建元素，并压栈 
		 * @param eleCls
		 * @param count
		 * @return 
		 * 
		 */		
		public function createElements(eleCls:Class,count:int = 1):LinkedStack
		{
			var linkedstick:LinkedStack;
			linkedstick = workshops[getClsName(eleCls)] ? workshops[getClsName(eleCls)] : new LinkedStack();
			for(var i:int = 0 ; i < count ; i++)
			{
				var instance:Object = new eleCls.prototype;
				linkedstick.push(instance);
			}
			return linkedstick;
		}
		/**
		 * 获取元素
		 * @param eleCls
		 * @param autoCreate
		 * @return 
		 * 
		 */		
		public function getElement(eleCls:Class, autoCreate:Boolean=false):IBase
		{
			var workshop:LinkedStack = workshops[getClsName(eleCls)];
			if(workshop && !workshop.isEmpty())
			{
				return workshop.pop();
			}else if(workshop && autoCreate){
				workshop = workshops[getClsName(eleCls)] = new LinkedStack();
			}
			return null;
		}
		/**
		 * 获取新元素 
		 * @param eleCls
		 * @param count
		 * @param autoCreate
		 * @return 
		 */		
		public function getElements(eleCls:Class, count:uint=1, autoCreate:Boolean=false):Vector.<IBase>
		{
			
			return null;
		}
		/**
		 * 闲置元素压栈 
		 * @param element
		 * @return 
		 * 
		 */		
		public function setback(element:Object):Boolean
		{
			var clsName:String = getClsName(element);
			if(workshops[clsName])
			{
				(workshops[clsName] as LinkedStack).push(element);
				return true;
			}
			return false;
		}
		private function getClsName(eleCls:Object):String
		{
			var clsName:String = getQualifiedClassName(eleCls);
			return clsName;
		}
	}
}